from random import randint

from utils.log import Log
from utils.generator import Generator
from utils.tree_node import TreeNode
from utils.qs import QS


class TreeGenerator(Log):
    def __init__(self, max_height=5):
        super(TreeGenerator, self).__init__()
        self.max_height = max_height

    def do(self):
        height = 0
        g = Generator()
        q = QS()
        root = TreeNode(g.single_val())
        q.push(root)

        while len(q) and height < self.max_height:
            size = len(q)
            for _ in range(size):
                n = q.pop()
                left = randint(0, 2)
                if left:
                    n.left = TreeNode(g.single_val())
                    # self.info(f"push left {n.left}")
                    q.push(n.left)
                right = randint(0, 2)
                if right:
                    n.right = TreeNode(g.single_val())
                    # self.info(f"push right {n.right}")
                    q.push(n.right)
            height += 1
        return root
